package webjoch.process.strategies.puzzles;

import java.awt.Graphics2D;

import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.script.methods.Calculations;
import com.rsbuddy.script.methods.Npcs;
import com.rsbuddy.script.methods.Objects;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.methods.Widgets;
import com.rsbuddy.script.task.Task;
import com.rsbuddy.script.util.Filter;
import com.rsbuddy.script.wrappers.GameObject;
import com.rsbuddy.script.wrappers.Npc;
import com.rsbuddy.script.wrappers.Tile;

import webjoch.misc.Functions;
import webjoch.process.strategies.Puzzle;
import webjoch.wrappers.Room;
/**
 * 
 * @author Webjoch
 *
 */
public class Emote extends Puzzle{
	Room room;
	boolean solved;
	
	private int[] STATUE = {10966};
	private int[] PRESURE_PAD = {52206};
	
	@Override
	public void setRoom(Room room) {
		this.room = room;
		
	}

	@Override
	public boolean isValid() {
		Npc[] statue = Npcs.getLoaded(new Filter<Npc>(){
			public boolean accept(Npc n){
				return room.getArea().contains(n.getLocation()) && Functions.inArray(STATUE, n.getId());
			}
		});
		return statue.length > 0;
	}

	@Override
	public Type getType() {
		return Type.EMOTE;
	}

	@Override
	public int loop(){
		final Npc[] statue = Npcs.getLoaded(new Filter<Npc>(){
			public boolean accept(Npc n){
				return room.getArea().contains(n.getLocation()) && Functions.inArray(STATUE, n.getId());
			}
		});
		if (statue.length == 0) return -1;
		
		Tile presurePad = Objects.getLoaded(new Filter<GameObject>(){
			public boolean accept(GameObject o){
				return Calculations.distanceBetween(statue[0].getLocation(), o.getLocation()) == 3 && Functions.inArray(PRESURE_PAD, o.getId());
			}
			
		})[0].getLocation();
		
		if (!Players.getLocal().getLocation().equals(presurePad)){
			if (!presurePad.interact("Walk here")){
				presurePad.clickOnMap();
			}
			Task.sleep(1500);
			return 1;
		}
		
		boolean animation = statue[0].getAnimation() > 0;
		if (statue[0].getAnimation() == 863) Widgets.getComponent(238, 1).click();
		if (statue[0].getAnimation() == 855) Widgets.getComponent(238, 2).click();
		if (statue[0].getAnimation() == 856) Widgets.getComponent(238, 3).click();
		if (statue[0].getAnimation() == 861) Widgets.getComponent(238, 4).click();
		if (statue[0].getAnimation() == 860) Widgets.getComponent(238, 5).click();
		if (animation) Task.sleep(1000);
		return 0;
	}

	@Override
	public boolean isFinished() {
		for (Room solvedRoom : solvedRooms){
			if (solvedRoom.getLocation().equals(room.getLocation())) return true; 
		}
		if (solved) solvedRooms.add(room);
		return solved;
	}

	@Override
	public int[] itemNeed() {
		return new int[0];
	}

	@Override
	public void messageReceived(MessageEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onRepaint(Graphics2D g) {
		// TODO Auto-generated method stub
		
	}

}
